package messageControl;

import com.alibaba.fastjson.JSON;
import com.gy.room.AbstractRoom;
import com.gy.room.clientPlayInfo;
import com.gy.room.roomManager;
import com.gy.room.userManager;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import message.LoginMessage;
import message.MoveMessage;
import message.PutWritBallMessage;
import message.SetColorMessage;
import message.ShootEndMessage;
import message.ShootMessage;
import message.faceMessage;
import message.reslutMessage;

public class MessageAction {

	private static final MessageAction instance = new MessageAction();
	public static boolean isIogame = false;
	public int roomType = -1;

	public static MessageAction getInstance() {
		return instance;
	}
	
	public   void sendMessage(ChannelHandlerContext ctx,String str)
	{
		 ctx.channel().writeAndFlush(new TextWebSocketFrame(str));
	}
	
	public void c2sLogin(ChannelHandlerContext s, LoginMessage tmpLg) {
		
		//登录命令只要简单回复即可
		tmpLg.msgId=1101;
	    //判断一下 玩家是否已经在游戏中
	
		
		clientPlayInfo tmpUser=userManager.getInstance().createNewUser(s,tmpLg);
         
         
		roomManager.getInstance().enterWaitingRoom(tmpUser);
		tmpLg.setUserId(tmpUser.getUserId());
         
		sendMessage(s,JSON.toJSONString(tmpLg));
	}
	
public void c2sShoot(ChannelHandlerContext s, ShootMessage tmpLg) {
		
		//登录命令只要简单回复即可
	tmpLg.msgId=tmpLg.msgId-900;
	    //判断一下 玩家是否已经在游戏中
	
		
        int roomIDtmp=tmpLg.getRoomId();
         
         if(roomIDtmp<=0)
         {
             System.out.println("c2sShoot get roomID fail");
        	 return ;
         }
         AbstractRoom room=	roomManager.getInstance().getRoomById(roomIDtmp);
         if(room==null)
         {
        	 System.out.println("c2sShoot get room fail");
        	 return ;
         }
         
         
         room.broakcastShootInfo(tmpLg);
		
	}

public void c2sMove(ChannelHandlerContext s, MoveMessage tmpLg) {
	
	//登录命令只要简单回复即可
	tmpLg.msgId=tmpLg.msgId-900;
    //判断一下 玩家是否已经在游戏中

	
    int roomIDtmp=tmpLg.getRoomId();
     
     if(roomIDtmp<=0)
     {
         System.out.println("c2sShoot get roomID fail");
    	 return ;
     }
     AbstractRoom room=	roomManager.getInstance().getRoomById(roomIDtmp);
     if(room==null)
     {
    	 System.out.println("c2sShoot get room fail");
    	 return ;
     }
     
     
	
     room.broakcastMoveInfo(tmpLg);
}

public void c2sShootEnd(ChannelHandlerContext s, ShootEndMessage tmpLg) {
	
	//登录命令只要简单回复即可
	tmpLg.msgId=tmpLg.msgId-900;
    //判断一下 玩家是否已经在游戏中

	
    int roomIDtmp=tmpLg.getRoomId();
     
     if(roomIDtmp<=0)
     {
         System.out.println("c2sShoot get roomID fail");
    	 return ;
     }
     AbstractRoom room=	roomManager.getInstance().getRoomById(roomIDtmp);
     if(room==null)
     {
    	 System.out.println("c2sShoot get room fail");
    	 return ;
     }
     
     
     room.broakcastShootEnd(tmpLg);
	
}


public void c2sPutWriteBall(ChannelHandlerContext s, PutWritBallMessage tmpLg) {
	
	//登录命令只要简单回复即可
	tmpLg.msgId=tmpLg.msgId-900;
    //判断一下 玩家是否已经在游戏中

	
    int roomIDtmp=tmpLg.getRoomId();
     
     if(roomIDtmp<=0)
     {
         System.out.println("c2sShoot get roomID fail");
    	 return ;
     }
     AbstractRoom room=	roomManager.getInstance().getRoomById(roomIDtmp);
     if(room==null)
     {
    	 System.out.println("c2sShoot get room fail");
    	 return ;
     }
     
     
     
     room.broakcastPutBall(tmpLg);
	
}


public void c2sSetColor(ChannelHandlerContext s, SetColorMessage tmpLg) {
	
	//登录命令只要简单回复即可
	tmpLg.msgId=tmpLg.msgId-900;
    //判断一下 玩家是否已经在游戏中

	
    int roomIDtmp=tmpLg.getRoomId();
     
     if(roomIDtmp<=0)
     {
         System.out.println("c2sShoot get roomID fail");
    	 return ;
     }
     AbstractRoom room=	roomManager.getInstance().getRoomById(roomIDtmp);
     if(room==null)
     {
    	 System.out.println("c2sShoot get room fail");
    	 return ;
     }
     
     
     
     room.broakcastSetColor(tmpLg);
	
}
public void c2sGameOver(ChannelHandlerContext s, reslutMessage tmpLg) {
	
	//登录命令只要简单回复即可
	

	
    int roomIDtmp=tmpLg.getRoomId();
     
     if(roomIDtmp<=0)
     {
         System.out.println("c2sShoot get roomID fail");
    	 return ;
     }
     AbstractRoom room=	roomManager.getInstance().getRoomById(roomIDtmp);
     if(room==null)
     {
    	 System.out.println("c2sShoot get room fail");
    	 return ;
     }
     
     
     
     room.broakcastGameOverInfo(tmpLg);
	
}

public void c2sFace(ChannelHandlerContext s,faceMessage tmpLg, String str) {
	
	//登录命令只要简单回复即可
	tmpLg.msgId=tmpLg.msgId-900;
    //判断一下 玩家是否已经在游戏中

	
    int roomIDtmp=tmpLg.getRoomId();
     
     if(roomIDtmp<=0)
     {
         System.out.println("c2sShoot get roomID fail");
    	 return ;
     }
     AbstractRoom room=	roomManager.getInstance().getRoomById(roomIDtmp);
     if(room==null)
     {
    	 System.out.println("c2sShoot get room fail");
    	 return ;
     }
     
     
     
     room.broakcastface(tmpLg,str);
	
}




}
